/*
 * @lc app=leetcode.cn id=228 lang=typescript
 *
 * [228] 汇总区间
 */

// @lc code=start
function summaryRanges(nums: number[]): string[] {
    if (nums.length === 0) return [];
    let result: number[][] = [];
    let now: [number, number] = [nums[0], nums[0]];
    for (let index = 1; index < nums.length; index++) {
        const element = nums[index];
        if (element - 1 === now[1]) {
            // 符合条件，存储点向后移动一位
            now[1] = element;
        } else {
            // 不符合条件，存储数组，并写为这个数字开头
            result.push(now);
            now = [element, element];
        }
    }
    if (now.length) result.push(now);
    return result.map(([a, b]) => {
        if (a === b) {
            return a.toString();
        } else {
            return `${a}->${b}`;
        }
    });
}
// @lc code=end
